﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Collections.Specialized;

namespace PDO.Utilities
{
    public static class ConfigurationHelper
    {
        public static bool KeyExists(string key)
        {
            string keyValue = null;
            try
            {
                // Attempt to find the key in the app settings, if not found this will throw
                keyValue = ConfigurationManager.AppSettings[key];                
            }
            catch (KeyNotFoundException)
            {
            }

            // If the keyValue has not been set, then it is known that the key does not exist.
            return keyValue != null;
        }
        /// <summary>
        /// Read a configuration setting from the current applications settings section.
        /// </summary>        
        /// <remarks>
        /// Settings should be defined in the calling applications configuration file, in the appsettings section
        /// </remarks>
        /// <param name="key">Configuration key name</param>
        /// <param name="defaultValue">Default value to be returned </param>
        /// <returns>Configuration string defined under the specified key if the value exists, otherwise the default value</returns>
        public static string ReadConfigurationString(string key, string defaultValue)
        {
            try
            {
                if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings[key]))
                {
                    return ConfigurationManager.AppSettings[key];
                }
            }
            finally
            {
                // Catch and swallow so that the default value will be returned.
            }
            return defaultValue;
        }

        public static int ReadConfigurationStringAsInt(string configurationKey, int configurationSettingDefault)
        {
            int? configurationValue = null;
            try
            {
                string configString = ConfigurationHelper.ReadConfigurationString(configurationKey, configurationSettingDefault.ToString());
                configurationValue = int.Parse(configString);
            }
            catch (Exception e)
            {
                Logger.WriteException("Failed to read configuration string " + configurationKey, "ConfigurationHelper", e);
            }
            finally
            {
                if (configurationValue == null)
                {
                    configurationValue = configurationSettingDefault;
                }
            }
            return (int)configurationValue;

        }

        public static double ReadConfigurationStringAsDouble(string configurationKey, double configurationSettingDefault)
        {
            double ?configurationValue = null;
            try
            {
                string configString = ConfigurationHelper.ReadConfigurationString(configurationKey, configurationSettingDefault.ToString());
                configurationValue = double.Parse(configString);                
            }
            catch (Exception e)
            {
                Logger.WriteException("Failed to read configuration string " + configurationKey, "ConfigurationHelper", e);
            }
            finally
            {
                if (configurationValue == null)
                {
                    configurationValue = configurationSettingDefault;
                }
            }
            return (double)configurationValue;
        }


    }
}
